//$Header:$

package de.bk.timecalc.gui;

import java.text.NumberFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

/**
 * Utility stuff for the swt gui
 * 
 * @author Bastian Krol
 */
public class GuiToolbox
{

  private static final ThreadLocal<NumberFormat> NUMBER_FORMAT = new ThreadLocal<NumberFormat>()
  {
    @Override
    protected NumberFormat initialValue()
    {
      NumberFormat format = NumberFormat.getInstance();
      format.setMaximumFractionDigits(2);
      format.setMinimumFractionDigits(2);
      format.setGroupingUsed(true);
      return format;
    }
  };

  static void openOkPopup(Shell parent, String title, String message, int icon)
  {
    MessageBox messageBox = new MessageBox(parent, icon | SWT.OK);
    messageBox.setText(title);
    messageBox.setMessage(message);
    messageBox.open();
  }

  static Text createUneditableText(Composite composite, GridData gridData)
  {
    return createUneditableText(composite, gridData, SWT.CENTER);
  }

  static Text createUneditableText(Composite composite, GridData gridData,
      int horizontalAlignment)
  {
    Text text = createText(composite, gridData, horizontalAlignment);
    text.setEditable(false);
    return text;
  }

  static Text createText(Composite composite, GridData gridData,
      int horizontalAlignement)
  {
    Text text = new Text(composite, SWT.SINGLE | horizontalAlignement);
    text.setLayoutData(gridData);
    return text;
  }

  static Text createText(Composite composite, GridData gridData)
  {
    return createText(composite, gridData, SWT.CENTER);
  }

  static Label createLabel(Composite composite, GridData gridData, String text)
  {
    return createLabel(composite, gridData, text, SWT.CENTER);
  }

  static Label createLabel(Composite composite, GridData gridData, String text,
      int horizontalAlignment)
  {
    Label label = new Label(composite, horizontalAlignment);
    label.setText(text);
    label.setLayoutData(gridData);
    return label;
  }

  static Text createLabelAndField(Composite composite, String labelText,
      String tooltip, GridData gridDataLabel, GridData gridDataValue)
  {
    Label label = createLabel(composite, gridDataLabel, labelText, SWT.LEFT);
    label.setToolTipText(tooltip);
    Text text = createUneditableText(composite, gridDataValue, SWT.RIGHT);
    text.setToolTipText(tooltip);
    return text;
  }

  static Spinner createSpinner(Composite parent, GridData gridData)
  {
    Spinner spinner = new Spinner(parent, SWT.BORDER);
    spinner.setLayoutData(gridData);
    return spinner;
  }

  static Label createHorizontalSeparator(Composite composite, GridData gridData)
  {
    Label label = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
    label.setLayoutData(gridData);
    return label;
  }

  static Label createVerticalSeparator(Composite composite, GridData gridData)
  {
    Label label = new Label(composite, SWT.SEPARATOR | SWT.VERTICAL);
    label.setLayoutData(gridData);
    return label;
  }

  static String intToTwoDigitString(int value)
  {
    if (value < 0)
    {
      throw new RuntimeException(
          "intToTwoDigitString not allowed for negative values (value was ["
              + value + "]).");
    }
    else if (value < 10)
    {
      return "0" + String.valueOf(value);
    }
    else if (value >= 100)
    {
      throw new RuntimeException(
          "intToTwoDigitString not allowed for values >= 100 (value was ["
              + value + "]).");
    }
    else
    {
      return String.valueOf(value);
    }
  }

  static String floatToString(float f)
  {
    return NUMBER_FORMAT.get().format(f);
  }

  static abstract class DefaultSelectionListener implements SelectionListener
  {
    @Override
    public final void widgetDefaultSelected(SelectionEvent e)
    {
      this.widgetSelected(e);
    }
  }

  static Button createButton(Composite composite, String label)
  {
    Button button = new Button(composite, SWT.PUSH);
    button.setText(label);
    return button;
  }

  static Button createButton(Composite composite, String label,
      GridData gridData)
  {
    Button button = createButton(composite, label);
    button.setLayoutData(gridData);
    return button;
  }

  static Button createCheckbox(Composite composite, GridData gridData)
  {
    Button button = new Button(composite, SWT.CHECK);
    button.setLayoutData(gridData);
    return button;
  }
}
